package com.etime.service.impl;

import com.etime.mapper.OrderMapper;
import com.etime.pojo.Order;
import com.etime.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public PageInfo<Order> getOrderByPage(Integer page) {
        PageHelper.startPage(page,5);
        List<Order> list=orderMapper.getAllOrder();
        PageInfo<Order> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Order> getOrderByName(Integer page, String user_name) {
        PageHelper.startPage(page,5);
        List<Order> list=orderMapper.getOrderByName("%"+user_name+"%");
        PageInfo<Order> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addOrder(Order order) {
        if ("business".equals(order.getSeat_type())) {
            order.setSeat_type("商务座");
        } else if ("first".equals(order.getSeat_type())) {
            order.setSeat_type("一等座");
        } else if ("second".equals(order.getSeat_type())) {
            order.setSeat_type("二等座");
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将实践日期类型转换为字符串
        String dateTime = sdf.format(date);
        order.setOrder_time(dateTime);
        orderMapper.addOrder(order);
    }

    @Override
    public void updateStatus(Integer order_id) {
        orderMapper.updateStatus(order_id);
    }

    @Override
    public Order getOrderById(Integer order_id) {
        return orderMapper.getOrderById(order_id);
    }

    @Override
    public List<Order> getOrderByUser(Integer user_id) {
        return orderMapper.getOrderByUser(user_id);

    }
}
